﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using KingOfCSharp.iHunter.Database;
using KingOfCSharp.iHunter.Database.MasterDataSetTableAdapters;

namespace KingOfCSharp.iHunter
{
    class PluginClient : IPluginClient
    {
        LocalClient localClient;
        Guid pluginGuid;
        DataSet pluginDatabase;

        /// <summary>
        /// Initializes a new instance of the <see cref="PluginClient"/> class.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="guid">The GUID.</param>
        public PluginClient(LocalClient client, Guid guid)
        {
            localClient = client;
            pluginGuid = guid;
            pluginDatabase = null;
            localClient.PluginClientsDictionary[guid] = this;
        }

        #region IPluginClient Members


        /// <summary>
        /// Gets the plugin private database.
        /// </summary>
        /// <returns>The data set</returns>
        public System.Data.DataSet GetPluginDatabase()
        {
            if (this.pluginDatabase == null)
            {
                MasterDataSet.PluginsRow row = localClient.MasterDataSet.Plugins.FindByGuid(pluginGuid);
                if (row == null)
                {
                    return null;
                }

                //Check whether it has a data file
                //If not, create one
                if (row.DataStorePath == null)
                {
                    this.pluginDatabase = new DataSet();

                    string pluginDir = Path.Combine(Path.Combine((String)AppDomain.CurrentDomain.GetData("DataDirectory"), "Plugins"), pluginGuid.ToString());
                    if (!Directory.Exists(pluginDir))
                        Directory.CreateDirectory(pluginDir);
                    string dataFile = Path.Combine(pluginDir, "data.xml");
                    this.pluginDatabase.WriteXml(dataFile, XmlWriteMode.WriteSchema);

                    row.DataStorePath = dataFile;

                    return this.pluginDatabase;
                }
                else
                {
                    string dataFile = row.DataStorePath;
                    this.pluginDatabase = new DataSet();
                    this.pluginDatabase.ReadXml(dataFile);
                    return this.pluginDatabase;
                }
            }
            else
            {
                return this.pluginDatabase;
            }
        }

        /// <summary>
        /// Store plugin database
        /// </summary>
        /// <param name="dataset">The database</param>
        /// <returns>True if succeed, else false</returns>
        public bool StorePluginDatabase()
        {
            MasterDataSet.PluginsRow row = localClient.MasterDataSet.Plugins.FindByGuid(pluginGuid);
            if (row == null)
            {
                return false;
            }

            if (row.DataStorePath == null)
            {
                return false;
            }

            string dataFile = Path.Combine(row.PluginPath, row.DataStorePath);
            try
            {
                this.pluginDatabase.WriteXml(dataFile, XmlWriteMode.WriteSchema);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Gets the settings of this plugin
        /// </summary>
        /// <returns>The settings</returns>
        public string GetPluginSettings()
        {
            MasterDataSet.PluginsRow row = localClient.MasterDataSet.Plugins.FindByGuid(pluginGuid);
            if (row == null)
            {
                throw new InvalidOperationException(String.Format("Plugin {0} Not Found", pluginGuid));
            }
            return row.Settings;
        }


        /// <summary>
        /// Shows the notify message.
        /// </summary>
        /// <param name="message">The message.</param>
        public void ShowNotifyMessage(string message)
        {
            MessageSelected(this, new MessageEventArgs(null));
            throw new NotImplementedException();
        }

        public event MessageEventHandler MessageSelected;

        #endregion
    }
}
